package com.jxwifi.cloud.quickclean.mycenter;

import android.content.Context;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.popwindow.PopSexModule;
import com.jxwifi.cloud.quickclean.popwindow.PopShowDialogModule;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;

public class PersonalPresenter {

    private Context mContext;
    private PersonalView mView;
    private AuzImpl mAuz;

    public PersonalPresenter(Context context,PersonalView view){
        this.mContext=context;
        this.mView=view;
        mAuz=new AuzImpl();
    }

    public void outLogin(){
        mAuz.auzget(UrlPool.GET_LOGOUT, new Params(), new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {

            }

            @Override
            public void AuzFailure(int errorCode, String msg) {

            }
        });
    }

    public void profile(){
        Params params=new Params();
        params.add("avatar",mView.getUrl());
        mAuz.auzget(UrlPool.POST_UPDATE_PROFILE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    InfoBean mInfoBean= JSON.parseObject(data, InfoBean.class);
                    AccountMgr.putUser(mContext,mInfoBean,false);
                    Glide.with(mContext).load(mInfoBean.getHeadImg()).into(mView.getImageView());
                }else{
                    mView.onprofile();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //性别选择
    public void sexChoice(){
        PopSexModule mPopModule=new PopSexModule(mContext, new PopSexModule.PopSexListener() {
            @Override
            public void SexMan(String sexType) {
                mView.onSex(sexType);
                onSexrequest();
            }

            @Override
            public void SexWoman(String sexType) {
                mView.onSex(sexType);
                onSexrequest();

            }
        });
        Popwindows window = PopWindowUtil.getPopupWindow(mPopModule.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);
        mPopModule.setPopwindow(window);
        window.show(mView.getImageView(), Popwindows.Location.TOP);

    }

    //性别接口请求
    public void onSexrequest(){
        Params params=new Params();
        params.add("sex",mView.getSex());
        mAuz.auzget(UrlPool.POST_UPDATE_PROFILE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    InfoBean mInfoBean= JSON.parseObject(data, InfoBean.class);
                    AccountMgr.putUser(mContext,mInfoBean,false);
                    mView.onSuccess();
                }else{
                    mView.onSexrequest();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

}
